package frsf.cidisi.exercise.idemiatp.search.actions;

import frsf.cidisi.exercise.idemiatp.search.*;
import frsf.cidisi.faia.agent.search.SearchAction;
import frsf.cidisi.faia.agent.search.SearchBasedAgentState;
import frsf.cidisi.faia.state.AgentState;
import frsf.cidisi.faia.state.EnvironmentState;

public class IrPeregaey extends SearchAction {

    /**
     * This method updates a tree node state when the search process is running.
     * It does not updates the real world state.
     */
    @Override
    public SearchBasedAgentState execute(SearchBasedAgentState s) {
        StateArquelogo agState = (StateArquelogo) s;
        
        String ciudadActual = agState.getPosicion();
        
        if((ciudadActual == "Ergintone" || ciudadActual == "Vinizaile")&& 
        		!agState.getCiudadesVisitadas().contains("Peregaey")){
        	
        	agState.setPosicion("Peregaey");
        	
        	return agState;
        }
        
        return null;
    }

    /**
     * This method updates the agent state and the real world state.
     */
    @Override
    public EnvironmentState execute(AgentState ast, EnvironmentState est) {
        StateIsla environmentState = (StateIsla) est;
        StateArquelogo agState = ((StateArquelogo) ast);

        String ciudadActual = agState.getPosicion();
        
        if((ciudadActual == "Ergintone" || ciudadActual == "Vinizaile") && 
        		!agState.getCiudadesVisitadas().contains("Peregaey")) {
            
        	//agState.ciudadesVisitadas.add("Peregaey");
        	// Update the real world
    		
            environmentState.setPosicionArquelogo("Peregaey");
            agState.getCiudadesVisitadas().add("Peregaey");

            agState.setPosicion("Peregaey");
            
            return environmentState;
        }

        return null;
    }

    /**
     * This method returns the action cost.
     */
    @Override
    public Double getCost() {
        return new Double(0);
    }

    /**
     * This method is not important for a search based agent, but is essensial
     * when creating a calculus based one.
     */
    @Override
    public String toString() {
        return "IrPeregaey";
    }
}